<?php
namespace Admin\Controller;
use Think\Controller;
class ProductCarController extends PublicController{
	 public function index(){
	 	 $_SESSION['product_id']=I('get.id');
		 $this->display();
	 }

	 //获取首页汽车信息
	 public function get_product_list(){

	   $limit = I('get.limit');
       $page = I('get.page');
       $like = I('get.like');
   
       if($_SESSION['product_id']==1){
       	 	if(!empty($like)){
         		$where="a.name like '%".$like."%' or a.moblie like '%".$like."%' or a.address like '%".$like."%' and a.fenlei_id=".$_SESSION['product_id'];
       		}else{
       			$where="a.fenlei_id=".$_SESSION['product_id'];
       		}

       	 $ret['data'] = M('product')->alias('a')
	       ->join('left join __FENLEI__ as b on a.fenlei_id=b.id left join __GOODS_CAR_TYPE__ as c on a.goods_id=c.id')
	       ->field('a.id,a.fenlei_id,a.goods_id,b.name,c.car_img,c.car_name')
	       ->where($where)
	       ->page($page,$limit)
	       ->select();
	       foreach ($ret['data'] as $key => $value) {
	       	  $ret['data'][$key]['syname'] = $ret['data'][$key]['car_name'];
	          $ret['data'][$key]['logo'] = '<img style="width:50px;" src="'.$ret['data'][$key]['car_img'].'" >';
	        }

       }else if($_SESSION['product_id']==2){
       		if(!empty($like)){
         		$where="a.id like '%".$like."%' or b.name like '%".$like."%' or c.parts_title like '%".$like."%' and a.fenlei_id=".$_SESSION['product_id'];
       		}else{
       			$where="a.fenlei_id=".$_SESSION['product_id'];
       		}

   		   $ret['data'] = M('product')->alias('a')
	       ->join('left join __FENLEI__ as b on a.fenlei_id=b.id left join __GOODS_PARTS__ as c on a.goods_id=c.id')
	       ->field('a.id,a.fenlei_id,a.goods_id,b.name,c.parts_logo,c.parts_title')
	       ->where($where)
	       ->page($page,$limit)
	       ->select();
	       foreach ($ret['data'] as $key => $value) {
	       	  $ret['data'][$key]['syname'] = $ret['data'][$key]['parts_title'];
	          $ret['data'][$key]['logo'] = '<img style="width:50px;" src="'.$ret['data'][$key]['parts_logo'].'" >';
	        }
       }else if($_SESSION['product_id']==3){
       		if(!empty($like)){
         		$where="a.id like '%".$like."%' or b.name like '%".$like."%' or c.is_name like '%".$like."%' and a.fenlei_id=".$_SESSION['product_id'];
       		}else{
       			$where="a.fenlei_id=".$_SESSION['product_id'];
       		}

   		   $ret['data'] = M('product')->alias('a')
	       ->join('left join __FENLEI__ as b on a.fenlei_id=b.id left join __GOODS_INSURANCELIST__ as c on a.goods_id=c.id')
	       ->field('a.id,a.fenlei_id,a.goods_id,b.name,c.logo,c.is_name')
	       ->where($where)
	       ->page($page,$limit)
	       ->select();
	       foreach ($ret['data'] as $key => $value) {
	       	  $ret['data'][$key]['syname'] = $ret['data'][$key]['is_name'];
	          $ret['data'][$key]['logo'] = '<img style="width:50px;" src="'.$ret['data'][$key]['logo'].'" >';
	        }
       }
       

       $ret['total'] = M('product')->count();
       $ret['code'] = 200;
       $ret['msg'] = "查询成功";
       ret($ret);
	 }

	 //点击编辑显示信息
	 public function update(){
	 	$product_id=I('post.product_id');
	 	$fenlei_id=I('post.fenlei_id');
	 	$goods_id=I('post.goods_id');
	 	$fenlei=M('fenlei')->select();
	 	foreach ($fenlei as $key => $value) {
	 		if($fenlei[$key]['id']==$fenlei_id){
	 			$fenlei[$key]['selected']="selected";
	 		}else{
	 			$fenlei[$key]['selected']="";
	 		}
	 	}
	 	if($fenlei_id == 1){
	 		$goods=M('goods_car_type')->select();
	 		$productid=M('product')->where(['id'=>$product_id])->find();
		 	foreach ($goods as $key => $value) {
		 		if($goods[$key]['id']==$productid['goods_id']){
		 			$goods[$key]['selected']="selected";
		 		}else{
		 			$goods[$key]['selected']="";
		 	
		 		}
		 	}
		 	$data['fenlei']=$fenlei;
		 	$data['goods']=$goods;
		 	$data['isSuccess']=1;
	 	}else if($fenlei_id == 2){
	 		$parts=M('goods_parts')->select();
	 		$productid=M('product')->where(['id'=>$product_id])->find();
	 		foreach ($parts as $key => $value) {
	 			$parts[$key]['car_name']=$parts[$key]['parts_title'];
	 			if($parts[$key]['id']==$productid['goods_id']){
	 				$parts[$key]['selected']="selected";
	 			}else{
	 				$parts[$key]['selected']="";
	 			}
	 		}
		 	$data['fenlei']=$fenlei;
		 	$data['goods']=$parts;
		 	$data['isSuccess']=1;
	 	}else if($fenlei_id == 3){
	 		$insurancelist=M('goods_insurancelist')->select();
	 		$productid=M('product')->where(['id'=>$product_id])->find();
	 		foreach ($insurancelist as $key => $value) {
	 			$insurancelist[$key]['car_name']=$insurancelist[$key]['is_name'];
	 			if($insurancelist[$key]['id']==$productid['goods_id']){
	 				$insurancelist[$key]['selected']="selected";
	 			}else{
	 				$insurancelist[$key]['selected']="";
	 			}
	 		}
		 	$data['fenlei']=$fenlei;
		 	$data['goods']=$insurancelist;
		 	$data['isSuccess']=1;
	 	}
	 	
	 	ret($data);
	 }

	 //编辑提交
	 public function save(){
	 	$data['id']=I('post.id');
	 	$data['fenlei_id']=I('post.fenleiid');
	 	$data['goods_id']=I('post.goodsid');
	 	$re=M('product')->where(['id'=>$data['id']])->save($data);
	 	if($re!==false){
	 		$ret['isSuccess']=1;
	 		$ret['msg']="修改成功";
	 		$ret['id']=$_SESSION['product_id'];
	 		ret($ret);
	 	}else{
	 		$ret['isSuccess']=0;
	 		$ret['msg']="修改失败";
	 		$ret['id']=$_SESSION['product_id'];
	 		ret($ret);
	 	}
	 }

	 //删除
	 public function del(){
	 	$id=I('post.id');
	 	$re=M('product')->where(['id'=>$id])->del();
	 	if($re){
	 		$ret['isSuccess']=1;
	 		$ret['msg']="删除成功";
	 		ret($ret);
	 	}else{
	 		$ret['isSuccess']=0;
	 		$ret['msg']="删除失败";
	 		ret($ret);
	 	}

	 }
}
